use strict;
use LWP::UserAgent;
use JSON;
use Cwd;

my $thisScriptPath = getcwd;
my $testListFileName = "testList.txt";
my $testServiceAddress = "http://192.168.52.109:8080";
my $clientTestScriptPath = "clientTestScript";

my $json = JSON->new->utf8;
my $json_obj;
my $browser=LWP::UserAgent->new(agent=>'Mozilla/5.0');


#__END__

#读取测试列表数据
my @testList;
open(FILE,"<","$testListFileName")||die"cannot open the file: $testListFileName $!\n";
my @linelist = <FILE>;
close FILE;
foreach my $eachline(@linelist){
	next if($eachline =~ /^#|^\s+$/);
	push @testList, $eachline;
}

#int(1+rand (1000-1+1));


foreach my $eachline(@testList){
	#获取测试用例文件名
	my @arr = split /\s+/, $eachline;
	my $id = shift @arr;
	my $caseId = $id . time();	#用例的唯一id
	my $testCaseFileName = shift @arr;
	my $testDescription = join("", @arr);
	
	#通过用例文件名解析脚本中html文件名
	my @htmlList;
	my @functionList;
	open(FILE,"<","$testCaseFileName")||die"cannot open the file: $testCaseFileName $!\n";
	my @linelist = <FILE>;
	close FILE;
	foreach my $eachline1(@linelist){
		if($eachline1 =~ /^\/\/\@/){
			my @tempArr;
			my @tempArr = split /\s+/, $eachline1;
			push @htmlList, $tempArr[1];
			push @functionList, $tempArr[2];
		}
	}
	
	#请求测试服务，检查是否有异常
	my $returnInfo = $browser->get("$testServiceAddress/TestCaseControl?action=start&id=$caseId");
	my $code = $returnInfo->code;
	unless($code == 200){
		print "$testServiceAddress return $code, test can not start. bye!!";
		last;
	}
	#请求测试服务，告知已经开始测试
	my $start = $returnInfo->content;
	$json_obj = $json->decode("$start");
	if ($json_obj->{'result'} == 0){		#返回0说明已经在运行 跳过以下步骤执行下一条用例
		print $json_obj->{'message'} . "\n";
		next;
	}
	print "start to test $testCaseFileName\n";
	
	#找到html文件植入js脚本
	#组装html标记 onload="testPopLogin()"
	my $scriptMark = "\n" . '<script defer="defer" src="../'. $clientTestScriptPath . '/' . $testCaseFileName . '"></script>';
	my $scriptMark1 = "\n" . '<script defer="defer" src="../'. $clientTestScriptPath . '/testPublic.js"></script>';
	for(my $i=0; $i<$#htmlList+1; $i++){
		#进入html文件位置并读取
		chdir("../html/");
		open(ReadFILE,"<","$htmlList[$i]")||die"cannot open the file: $htmlList[$i] $!\n";
		my @linelist = <ReadFILE>;
		close ReadFILE;
		
		#组装onload属性 $attrOnload
		my $functionName = $functionList[$i];
		
		#替换时间错作为用例的唯一id来控制用例的启动
		if($functionName =~ /caseId/){
			$functionName =~ s/caseId/$caseId/;
		}
		#替换用例文件名
		if($functionName =~ /testFileName/){
			$functionName =~ s/testFileName/$testCaseFileName/;
		}
		#替换测试描述
		if($functionName =~ /description/){
			$functionName =~ s/description/$testDescription/;
		}
		my $attrOnload = ' onload="' . $functionName . '">';
		#print $attrOnload;
		
		#开始植入脚本.包括底部的两个script标签,与body中的onload属性
		push @linelist, $scriptMark;
		push @linelist, $scriptMark1;
		for(@linelist){
			my $eachline = $_;
			if($eachline =~ /(.*?)<body.*?(>)(.*?)/){
				$eachline =~ s/(.*?)<body(.*?)(>)(.*?)/$1<body$2$attrOnload$4/;
				$_ = $eachline;
			}
		}
		
		open(WriteFILE,">","$htmlList[$i]")||die"cannot open the file: $!\n";
		print WriteFILE $_ for(@linelist);
		close WriteFILE;
		
		
		#一个植入完回到本位
		chdir($thisScriptPath);
		
	}
	
	#print "----";
	#<>;
	
	#启动程序开始测试，并且循环请求 action=isEnd,当action=isEnd返回值为1时
	my $pid = fork();
	chdir("..");
	if($pid){
		system("Go-Goal.exe");
		exit;
	}
	
	chdir($thisScriptPath);
	while(1){
		my $isEnd = $browser->get("$testServiceAddress/TestCaseControl?action=isEnd&id=$caseId")->content;
		$json_obj = $json->decode("$isEnd");
		if($json_obj->{'result'} == 1){
			print "Test case run over.\n";
			last;
		}
		sleep 2;
	}
	
	
	#停止程序,移除html中js植入
	system("TASKKILL /F /IM Go-Goal.exe");
	
	for(my $i=0; $i<$#htmlList+1; $i++){
		#进入html文件位置并读取
		chdir("../html/");
		open(ReadFILE,"<","$htmlList[$i]")||die"cannot open the file: $!\n";
		my @linelist = <ReadFILE>;
		close ReadFILE;
		
		#开始移除脚本.包括底部的两个script标签,与body中的onload属性
		pop @linelist;
		pop @linelist;
		#移除最后一个换行符
		chomp($linelist[$#linelist]);
		for(@linelist){
			my $eachline = $_;
			if($eachline =~ /(.*?)<body.*?(onload=".*?>)(.*?)/){
				$eachline =~ s/(.*?)<body(.*?)( onload=".*?>)(.*?)/$1<body$2>$4/;
				$_ = $eachline;
			}
		}
		open(WriteFILE,">","$htmlList[$i]")||die"cannot open the file: $!\n";
		print WriteFILE $_ for(@linelist);
		close WriteFILE;
		
		#一个植入完回到本位
		chdir($thisScriptPath);
		
	}

	print "\n";
}

print "Test End \n";
<>;
